package com.snatch.snatchmgr.core;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.snatch.common.vo.NodeTaskInput;

@Component
@Scope ("prototype")
public class SnatchItemCore {
	
	/**
	 * 任务对列
	 */
	public ConcurrentLinkedQueue<String> dataQueue = new ConcurrentLinkedQueue<String>();
	
	@Autowired
	private SnatchItemThread snatchItemThread;
	
	@Autowired
	private SnatchDataThread snatchDataThread;
	

	public void initSnatchItemCore(NodeTaskInput nodeTaskInput){
		snatchItemThread.setNodeTaskInput(nodeTaskInput);
		
		snatchDataThread.setNodeTaskInput(nodeTaskInput);
		snatchDataThread.setDataQueue(dataQueue);
	}
	
	public void startSnatchItemThread(){
		snatchItemThread.start();
		snatchDataThread.start();
	}
	
	public void destroySnatchItemCore() {
		if(null !=snatchItemThread){
			snatchItemThread.destroySnatchItemThread();
			snatchItemThread = null;
		}
		if(null !=snatchDataThread){
			snatchDataThread.destroySnatchItemThread();
			snatchDataThread = null;
		}
		dataQueue.clear();
		dataQueue = null;
	}
	
	
	public void notifyAllQueue(String data){
		synchronized (dataQueue) {
			dataQueue.add(data);
			dataQueue.notifyAll();
		}
	}
	
	
}
